package com.xxwy.lifecycle.bean;

import lombok.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;

/**
 * bean.
 *
 * @author xxwy
 * @date 2021/7/13 0013
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User implements InitializingBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware, EnvironmentAware, MessageSourceAware, SmartInitializingSingleton {

    private String name;

    private Integer age;

    private String number;

    private BeanFactory beanFactory;

    private String beanName;

    private ApplicationContext applicationContext;

    private Environment environment;

    private MessageSource messageSource;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    /**
     * PostConstruct 的实现CommonAnnotationBeanPostProcessor实现的
     */
    @PostConstruct
    public void postConstruct() {
        System.out.println(beanName + " postConstruct");
    }

    private void init() {
        System.out.println(beanName + "  init-method");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(beanName + " InitializingBean#afterPropertiesSet");
    }

    @Override
    public void afterSingletonsInstantiated() {
        System.out.println(beanName+ " smartInitializingSingleton#afterSingletonInstantiated");
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("User finalize");
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
}
